home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / libogg / libvorbis-1.0rc3 / lib / block.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-27  |  23.5 KB  |  816 lines

  1. /********************************************************************
  2.  *                                                                  *
  3.  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
  4.  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
  5.  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6.  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
  7.  *                                                                  *
  8.  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
  9.  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
  10.  *                                                                  *
  11.  ********************************************************************
  12.  
  13.  function: PCM data vector blocking, windowing and dis/reassembly
  14.  last mod: $Id: block.c,v 1.55 2001/12/23 11:53:52 xiphmont Exp $
  15.  
  16.  Handle windowing, overlap-add, etc of the PCM vectors.  This is made
  17.  more amusing by Vorbis' current two allowed block sizes.
  18.  
  19.  ********************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ogg/ogg.h>
  25. #include "vorbis/codec.h"
  26. #include "codec_internal.h"
  27.  
  28. #include "window.h"
  29. #include "mdct.h"
  30. #include "lpc.h"
  31. #include "registry.h"
  32. #include "misc.h"
  33.  
  34. static int ilog2(unsigned int v){
  35.   int ret=0;
  36.   while(v>1){
  37.     ret++;
  38.     v>>=1;
  39.   }
  40.   return(ret);
  41. }
  42.  
  43. /* pcm accumulator examples (not exhaustive):
  44.  
  45.  <-------------- lW ---------------->
  46.                    <--------------- W ---------------->
  47. :            .....|.....       _______________         |
  48. :        .'''     |     '''_---      |       |\        |
  49. :.....'''         |_____--- '''......|       | \_______|
  50. :.................|__________________|_______|__|______|
  51.                   |<------ Sl ------>|      > Sr <     |endW
  52.                   |beginSl           |endSl  |  |endSr   
  53.                   |beginW            |endlW  |beginSr
  54.  
  55.  
  56.                       |< lW >|       
  57.                    <--------------- W ---------------->
  58.                   |   |  ..  ______________            |
  59.                   |   | '  `/        |     ---_        |
  60.                   |___.'___/`.       |         ---_____| 
  61.                   |_______|__|_______|_________________|
  62.                   |      >|Sl|<      |<------ Sr ----->|endW
  63.                   |       |  |endSl  |beginSr          |endSr
  64.                   |beginW |  |endlW                     
  65.                   mult[0] |beginSl                     mult[n]
  66.  
  67.  <-------------- lW ----------------->
  68.                           |<--W-->|                               
  69. :            ..............  ___  |   |                    
  70. :        .'''             |`/   \ |   |                       
  71. :.....'''                 |/`....\|...|                    
  72. :.........................|___|___|___|                  
  73.                           |Sl |Sr |endW    
  74.                           |   |   |endSr
  75.                           |   |beginSr
  76.                           |   |endSl
  77.               |beginSl
  78.               |beginW
  79. */
  80.  
  81. /* block abstraction setup *********************************************/
  82.  
  83. #ifndef WORD_ALIGN
  84. #define WORD_ALIGN 8
  85. #endif
  86.  
  87. int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
  88.   memset(vb,0,sizeof(*vb));
  89.   vb->vd=v;
  90.   vb->localalloc=0;
  91.   vb->localstore=NULL;
  92.   if(v->analysisp){
  93.     vorbis_block_internal *vbi=
  94.       vb->internal=_ogg_calloc(1,sizeof(vorbis_block_internal));
  95.     oggpack_writeinit(&vb->opb);
  96.     vbi->ampmax=-9999;
  97.     vbi->packet_markers=_ogg_malloc(vorbis_bitrate_maxmarkers()*
  98.                    sizeof(*vbi->packet_markers));
  99.   }
  100.   
  101.   return(0);
  102. }
  103.  
  104. void *_vorbis_block_alloc(vorbis_block *vb,long bytes){
  105.   bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1);
  106.   if(bytes+vb->localtop>vb->localalloc){
  107.     /* can't just _ogg_realloc... there are outstanding pointers */
  108.     if(vb->localstore){
  109.       struct alloc_chain *link=_ogg_malloc(sizeof(*link));
  110.       vb->totaluse+=vb->localtop;
  111.       link->next=vb->reap;
  112.       link->ptr=vb->localstore;
  113.       vb->reap=link;
  114.     }
  115.     /* highly conservative */
  116.     vb->localalloc=bytes;
  117.     vb->localstore=_ogg_malloc(vb->localalloc);
  118.     vb->localtop=0;
  119.   }
  120.   {
  121.     void *ret=(void *)(((char *)vb->localstore)+vb->localtop);
  122.     vb->localtop+=bytes;
  123.     return ret;
  124.   }
  125. }
  126.  
  127. /* reap the chain, pull the ripcord */
  128. void _vorbis_block_ripcord(vorbis_block *vb){
  129.   /* reap the chain */
  130.   struct alloc_chain *reap=vb->reap;
  131.   while(reap){
  132.     struct alloc_chain *next=reap->next;
  133.     _ogg_free(reap->ptr);
  134.     memset(reap,0,sizeof(*reap));
  135.     _ogg_free(reap);
  136.     reap=next;
  137.   }
  138.   /* consolidate storage */
  139.   if(vb->totaluse){
  140.     vb->localstore=_ogg_realloc(vb->localstore,vb->totaluse+vb->localalloc);
  141.     vb->localalloc+=vb->totaluse;
  142.     vb->totaluse=0;
  143.   }
  144.  
  145.   /* pull the ripcord */
  146.   vb->localtop=0;
  147.   vb->reap=NULL;
  148. }
  149.  
  150. int vorbis_block_clear(vorbis_block *vb){
  151.   if(vb->vd)
  152.     if(vb->vd->analysisp)
  153.       oggpack_writeclear(&vb->opb);
  154.   _vorbis_block_ripcord(vb);
  155.   if(vb->localstore)_ogg_free(vb->localstore);
  156.  
  157.   if(vb->internal){
  158.     vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
  159.     if(vbi->packet_markers)_ogg_free(vbi->packet_markers);
  160.  
  161.     _ogg_free(vb->internal);
  162.   }
  163.  
  164.   memset(vb,0,sizeof(*vb));
  165.   return(0);
  166. }
  167.  
  168. /* Analysis side code, but directly related to blocking.  Thus it's
  169.    here and not in analysis.c (which is for analysis transforms only).
  170.    The init is here because some of it is shared */
  171.  
  172. static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){
  173.   int i;
  174.   codec_setup_info *ci=vi->codec_setup;
  175.   backend_lookup_state *b=NULL;
  176.  
  177.   memset(v,0,sizeof(*v));
  178.   b=v->backend_state=_ogg_calloc(1,sizeof(*b));
  179.  
  180.   v->vi=vi;
  181.   b->modebits=ilog2(ci->modes);
  182.  
  183.   b->transform[0]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[0]));
  184.   b->transform[1]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[1]));
  185.  
  186.   /* MDCT is tranform 0 */
  187.  
  188.   b->transform[0][0]=_ogg_calloc(1,sizeof(mdct_lookup));
  189.   b->transform[1][0]=_ogg_calloc(1,sizeof(mdct_lookup));
  190.   mdct_init(b->transform[0][0],ci->blocksizes[0]);
  191.   mdct_init(b->transform[1][0],ci->blocksizes[1]);
  192.  
  193.   b->window[0][0][0]=_ogg_calloc(VI_WINDOWB,sizeof(*b->window[0][0][0]));
  194.   b->window[0][0][1]=b->window[0][0][0];
  195.   b->window[0][1][0]=b->window[0][0][0];
  196.   b->window[0][1][1]=b->window[0][0][0];
  197.   b->window[1][0][0]=_ogg_calloc(VI_WINDOWB,sizeof(*b->window[1][0][0]));
  198.   b->window[1][0][1]=_ogg_calloc(VI_WINDOWB,sizeof(*b->window[1][0][1]));
  199.   b->window[1][1][0]=_ogg_calloc(VI_WINDOWB,sizeof(*b->window[1][1][0]));
  200.   b->window[1][1][1]=_ogg_calloc(VI_WINDOWB,sizeof(*b->window[1][1][1]));
  201.  
  202.   for(i=0;i<VI_WINDOWB;i++){
  203.     b->window[0][0][0][i]=
  204.       _vorbis_window(i,ci->blocksizes[0],ci->blocksizes[0]/2,ci->blocksizes[0]/2);
  205.     b->window[1][0][0][i]=
  206.       _vorbis_window(i,ci->blocksizes[1],ci->blocksizes[0]/2,ci->blocksizes[0]/2);
  207.     b->window[1][0][1][i]=
  208.       _vorbis_window(i,ci->blocksizes[1],ci->blocksizes[0]/2,ci->blocksizes[1]/2);
  209.     b->window[1][1][0][i]=
  210.       _vorbis_window(i,ci->blocksizes[1],ci->blocksizes[1]/2,ci->blocksizes[0]/2);
  211.     b->window[1][1][1][i]=
  212.       _vorbis_window(i,ci->blocksizes[1],ci->blocksizes[1]/2,ci->blocksizes[1]/2);
  213.   }
  214.  
  215.   if(encp){ /* encode/decode differ here */
  216.     /* finish the codebooks */
  217.     b->fullbooks=_ogg_calloc(ci->books,sizeof(*b->fullbooks));
  218.     for(i=0;i<ci->books;i++)
  219.       vorbis_book_init_encode(b->fullbooks+i,ci->book_param[i]);
  220.     v->analysisp=1;
  221.   }else{
  222.     /* finish the codebooks */
  223.     b->fullbooks=_ogg_calloc(ci->books,sizeof(*b->fullbooks));
  224.     for(i=0;i<ci->books;i++)
  225.       vorbis_book_init_decode(b->fullbooks+i,ci->book_param[i]);
  226.   }
  227.  
  228.   /* initialize the storage vectors to a decent size greater than the
  229.      minimum */
  230.   
  231.   v->pcm_storage=8192; /* we'll assume later that we have
  232.               a minimum of twice the blocksize of
  233.               accumulated samples in analysis */
  234.   v->pcm=_ogg_malloc(vi->channels*sizeof(*v->pcm));
  235.   v->pcmret=_ogg_malloc(vi->channels*sizeof(*v->pcmret));
  236.   {
  237.     int i;
  238.     for(i=0;i<vi->channels;i++)
  239.       v->pcm[i]=_ogg_calloc(v->pcm_storage,sizeof(*v->pcm[i]));
  240.   }
  241.  
  242.   /* all 1 (large block) or 0 (small block) */
  243.   /* explicitly set for the sake of clarity */
  244.   v->lW=0; /* previous window size */
  245.   v->W=0;  /* current window size */
  246.  
  247.   /* all vector indexes */
  248.   v->centerW=ci->blocksizes[1]/2;
  249.  
  250.   v->pcm_current=v->centerW;
  251.  
  252.   /* initialize all the mapping/backend lookups */
  253.   b->mode=_ogg_calloc(ci->modes,sizeof(*b->mode));
  254.   for(i=0;i<ci->modes;i++){
  255.     int mapnum=ci->mode_param[i]->mapping;
  256.     int maptype=ci->map_type[mapnum];
  257.     b->mode[i]=_mapping_P[maptype]->look(v,ci->mode_param[i],
  258.                      ci->map_param[mapnum]);
  259.   }
  260.  
  261.   return(0);
  262. }
  263.  
  264. /* arbitrary settings and spec-mandated numbers get filled in here */
  265. int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
  266.   backend_lookup_state *b=NULL;
  267.  
  268.   _vds_shared_init(v,vi,1);
  269.   b=v->backend_state;
  270.   b->psy_g_look=_vp_global_look(vi);
  271.  
  272.   /* Initialize the envelope state storage */
  273.   b->ve=_ogg_calloc(1,sizeof(*b->ve));
  274.   _ve_envelope_init(b->ve,vi);
  275.  
  276.   vorbis_bitrate_init(vi,&b->bms);
  277.  
  278.   return(0);
  279. }
  280.  
  281. void vorbis_dsp_clear(vorbis_dsp_state *v){
  282.   int i,j,k;
  283.   if(v){
  284.     vorbis_info *vi=v->vi;
  285.     codec_setup_info *ci=(vi?vi->codec_setup:NULL);
  286.     backend_lookup_state *b=v->backend_state;
  287.  
  288.     if(b){
  289.       if(b->window[0][0][0]){
  290.     for(i=0;i<VI_WINDOWB;i++)
  291.       if(b->window[0][0][0][i])_ogg_free(b->window[0][0][0][i]);
  292.     _ogg_free(b->window[0][0][0]);
  293.     
  294.     for(j=0;j<2;j++)
  295.       for(k=0;k<2;k++){
  296.         for(i=0;i<VI_WINDOWB;i++)
  297.           if(b->window[1][j][k][i])_ogg_free(b->window[1][j][k][i]);
  298.         _ogg_free(b->window[1][j][k]);
  299.       }
  300.       }
  301.  
  302.       if(b->ve){
  303.     _ve_envelope_clear(b->ve);
  304.     _ogg_free(b->ve);
  305.       }
  306.  
  307.       if(b->transform[0]){
  308.     mdct_clear(b->transform[0][0]);
  309.     _ogg_free(b->transform[0][0]);
  310.     _ogg_free(b->transform[0]);
  311.       }
  312.       if(b->transform[1]){
  313.     mdct_clear(b->transform[1][0]);
  314.     _ogg_free(b->transform[1][0]);
  315.     _ogg_free(b->transform[1]);
  316.       }
  317.       if(b->psy_g_look)_vp_global_free(b->psy_g_look);
  318.       vorbis_bitrate_clear(&b->bms);
  319.     }
  320.     
  321.     if(v->pcm){
  322.       for(i=0;i<vi->channels;i++)
  323.     if(v->pcm[i])_ogg_free(v->pcm[i]);
  324.       _ogg_free(v->pcm);
  325.       if(v->pcmret)_ogg_free(v->pcmret);
  326.     }
  327.  
  328.     /* free mode lookups; these are actually vorbis_look_mapping structs */
  329.     if(ci){
  330.       for(i=0;i<ci->modes;i++){
  331.     int mapnum=ci->mode_param[i]->mapping;
  332.     int maptype=ci->map_type[mapnum];
  333.     if(b && b->mode)_mapping_P[maptype]->free_look(b->mode[i]);
  334.       }
  335.       /* free codebooks */
  336.       for(i=0;i<ci->books;i++)
  337.     if(b && b->fullbooks)vorbis_book_clear(b->fullbooks+i);
  338.     }
  339.  
  340.     if(b){
  341.       if(b->mode)_ogg_free(b->mode);    
  342.       if(b->fullbooks)_ogg_free(b->fullbooks);
  343.       
  344.       /* free header, header1, header2 */
  345.       if(b->header)_ogg_free(b->header);
  346.       if(b->header1)_ogg_free(b->header1);
  347.       if(b->header2)_ogg_free(b->header2);
  348.       _ogg_free(b);
  349.     }
  350.     
  351.     memset(v,0,sizeof(*v));
  352.   }
  353. }
  354.  
  355. float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
  356.   int i;
  357.   vorbis_info *vi=v->vi;
  358.   backend_lookup_state *b=v->backend_state;
  359.  
  360.   /* free header, header1, header2 */
  361.   if(b->header)_ogg_free(b->header);b->header=NULL;
  362.   if(b->header1)_ogg_free(b->header1);b->header1=NULL;
  363.   if(b->header2)_ogg_free(b->header2);b->header2=NULL;
  364.  
  365.   /* Do we have enough storage space for the requested buffer? If not,
  366.      expand the PCM (and envelope) storage */
  367.     
  368.   if(v->pcm_current+vals>=v->pcm_storage){
  369.     v->pcm_storage=v->pcm_current+vals*2;
  370.    
  371.     for(i=0;i<vi->channels;i++){
  372.       v->pcm[i]=_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i]));
  373.     }
  374.   }
  375.  
  376.   for(i=0;i<vi->channels;i++)
  377.     v->pcmret[i]=v->pcm[i]+v->pcm_current;
  378.     
  379.   return(v->pcmret);
  380. }
  381.  
  382. static void _preextrapolate_helper(vorbis_dsp_state *v){
  383.   int i;
  384.   int order=32;
  385.   float *lpc=alloca(order*sizeof(*lpc));
  386.   float *work=alloca(v->pcm_current*sizeof(*work));
  387.   long j;
  388.   v->preextrapolate=1;
  389.  
  390.   if(v->pcm_current-v->centerW>order*2){ /* safety */
  391.     for(i=0;i<v->vi->channels;i++){
  392.       /* need to run the extrapolation in reverse! */
  393.       for(j=0;j<v->pcm_current;j++)
  394.     work[j]=v->pcm[i][v->pcm_current-j-1];
  395.       
  396.       /* prime as above */
  397.       vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order);
  398.       
  399.       /* run the predictor filter */
  400.       vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order,
  401.              order,
  402.              work+v->pcm_current-v->centerW,
  403.              v->centerW);
  404.  
  405.       for(j=0;j<v->pcm_current;j++)
  406.     v->pcm[i][v->pcm_current-j-1]=work[j];
  407.  
  408.     }
  409.   }
  410. }
  411.  
  412.  
  413. /* call with val<=0 to set eof */
  414.  
  415. int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
  416.   vorbis_info *vi=v->vi;
  417.   codec_setup_info *ci=vi->codec_setup;
  418.   /*backend_lookup_state *b=v->backend_state;*/
  419.  
  420.   if(vals<=0){
  421.     int order=32;
  422.     int i;
  423.     float *lpc=alloca(order*sizeof(*lpc));
  424.  
  425.     /* if it wasn't done earlier (very short sample) */
  426.     if(!v->preextrapolate)
  427.       _preextrapolate_helper(v);
  428.  
  429.     /* We're encoding the end of the stream.  Just make sure we have
  430.        [at least] a full block of zeroes at the end. */
  431.     /* actually, we don't want zeroes; that could drop a large
  432.        amplitude off a cliff, creating spread spectrum noise that will
  433.        suck to encode.  Extrapolate for the sake of cleanliness. */
  434.  
  435.     vorbis_analysis_buffer(v,ci->blocksizes[1]*2);
  436.     v->eofflag=v->pcm_current;
  437.     v->pcm_current+=ci->blocksizes[1]*2;
  438.  
  439.     for(i=0;i<vi->channels;i++){
  440.       if(v->eofflag>order*2){
  441.     /* extrapolate with LPC to fill in */
  442.     long n;
  443.  
  444.     /* make a predictor filter */
  445.     n=v->eofflag;
  446.     if(n>ci->blocksizes[1])n=ci->blocksizes[1];
  447.     vorbis_lpc_from_data(v->pcm[i]+v->eofflag-n,lpc,n,order);
  448.  
  449.     /* run the predictor filter */
  450.     vorbis_lpc_predict(lpc,v->pcm[i]+v->eofflag-order,order,
  451.                v->pcm[i]+v->eofflag,v->pcm_current-v->eofflag);
  452.       }else{
  453.     /* not enough data to extrapolate (unlikely to happen due to
  454.            guarding the overlap, but bulletproof in case that
  455.            assumtion goes away). zeroes will do. */
  456.     memset(v->pcm[i]+v->eofflag,0,
  457.            (v->pcm_current-v->eofflag)*sizeof(*v->pcm[i]));
  458.  
  459.       }
  460.     }
  461.   }else{
  462.  
  463.     if(v->pcm_current+vals>v->pcm_storage)
  464.       return(OV_EINVAL);
  465.  
  466.     v->pcm_current+=vals;
  467.  
  468.     /* we may want to reverse extrapolate the beginning of a stream
  469.        too... in case we're beginning on a cliff! */
  470.     /* clumsy, but simple.  It only runs once, so simple is good. */
  471.     if(!v->preextrapolate && v->pcm_current-v->centerW>ci->blocksizes[1])
  472.       _preextrapolate_helper(v);
  473.  
  474.   }
  475.   return(0);
  476. }
  477.  
  478. /* do the deltas, envelope shaping, pre-echo and determine the size of
  479.    the next block on which to continue analysis */
  480. int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
  481.   int i;
  482.   vorbis_info *vi=v->vi;
  483.   codec_setup_info *ci=vi->codec_setup;
  484.   backend_lookup_state *b=v->backend_state;
  485.   vorbis_look_psy_global *g=b->psy_g_look;
  486.   vorbis_info_psy_global *gi=&ci->psy_g_param;
  487.   long beginW=v->centerW-ci->blocksizes[v->W]/2,centerNext;
  488.   vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
  489.  
  490.   /* check to see if we're started... */
  491.   if(!v->preextrapolate)return(0);
  492.  
  493.   /* check to see if we're done... */
  494.   if(v->eofflag==-1)return(0);
  495.  
  496.   /* By our invariant, we have lW, W and centerW set.  Search for
  497.      the next boundary so we can determine nW (the next window size)
  498.      which lets us compute the shape of the current block's window */
  499.   
  500.   if(ci->blocksizes[0]<ci->blocksizes[1]){
  501.     long bp=_ve_envelope_search(v);
  502.     if(bp==-1)return(0); /* not enough data currently to search for a
  503.                             full long block */
  504.     v->nW=bp;
  505.  
  506.   }else
  507.     v->nW=0;
  508.   
  509.   centerNext=v->centerW+ci->blocksizes[v->W]/4+ci->blocksizes[v->nW]/4;
  510.  
  511.   {
  512.     /* center of next block + next block maximum right side. */
  513.  
  514.     long blockbound=centerNext+ci->blocksizes[v->nW]/2;
  515.     if(v->pcm_current<blockbound)return(0); /* not enough data yet;
  516.                                                although this check is
  517.                                                less strict that the
  518.                                                _ve_envelope_search,
  519.                                                the search is not run
  520.                                                if we only use one
  521.                                                block size */
  522.  
  523.  
  524.   }
  525.   
  526.   /* fill in the block.  Note that for a short window, lW and nW are *short*
  527.      regardless of actual settings in the stream */
  528.  
  529.   _vorbis_block_ripcord(vb);
  530.   if(v->W){
  531.     vb->lW=v->lW;
  532.     vb->W=v->W;
  533.     vb->nW=v->nW;
  534.   }else{
  535.     vb->lW=0;
  536.     vb->W=v->W;
  537.     vb->nW=0;
  538.   }
  539.  
  540.   if(v->W){
  541.     if(!v->lW || !v->nW)
  542.       vbi->blocktype=BLOCKTYPE_TRANSITION;
  543.     else
  544.       vbi->blocktype=BLOCKTYPE_LONG;
  545.   }else{
  546.     if(_ve_envelope_mark(v))
  547.       vbi->blocktype=BLOCKTYPE_IMPULSE;
  548.     else
  549.       vbi->blocktype=BLOCKTYPE_PADDING;
  550.   }
  551.  
  552.   vb->vd=v;
  553.   vb->sequence=v->sequence++;
  554.   vb->granulepos=v->granulepos;
  555.   vb->pcmend=ci->blocksizes[v->W];
  556.   
  557.   /* copy the vectors; this uses the local storage in vb */
  558.  
  559.   /* this tracks 'strongest peak' for later psychoacoustics */
  560.   /* moved to the global psy state; clean this mess up */
  561.   if(vbi->ampmax>g->ampmax)g->ampmax=vbi->ampmax;
  562.   g->ampmax=_vp_ampmax_decay(g->ampmax,v);
  563.   vbi->ampmax=g->ampmax;
  564.   
  565.   vb->pcm=_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels);
  566.   vbi->pcmdelay=_vorbis_block_alloc(vb,sizeof(*vbi->pcmdelay)*vi->channels);
  567.   for(i=0;i<vi->channels;i++){
  568.     vbi->pcmdelay[i]=
  569.       _vorbis_block_alloc(vb,(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
  570.     memcpy(vbi->pcmdelay[i],v->pcm[i],(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
  571.     vb->pcm[i]=vbi->pcmdelay[i]+beginW;
  572.     
  573.     /* before we added the delay 
  574.        vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i]));
  575.        memcpy(vb->pcm[i],v->pcm[i]+beginW,ci->blocksizes[v->W]*sizeof(*vb->pcm[i]));
  576.     */
  577.     
  578.   }
  579.   
  580.   /* handle eof detection: eof==0 means that we've not yet received EOF
  581.                            eof>0  marks the last 'real' sample in pcm[]
  582.                            eof<0  'no more to do'; doesn't get here */
  583.  
  584.   if(v->eofflag){
  585.     if(v->centerW>=v->eofflag){
  586.       v->eofflag=-1;
  587.       vb->eofflag=1;
  588.       return(1);
  589.     }
  590.   }
  591.  
  592.   /* advance storage vectors and clean up */
  593.   {
  594.     int new_centerNext=ci->blocksizes[1]/2+gi->delaycache;
  595.     int movementW=centerNext-new_centerNext;
  596.  
  597.     if(movementW>0){
  598.  
  599.       _ve_envelope_shift(b->ve,movementW);
  600.       v->pcm_current-=movementW;
  601.       
  602.       for(i=0;i<vi->channels;i++)
  603.     memmove(v->pcm[i],v->pcm[i]+movementW,
  604.         v->pcm_current*sizeof(*v->pcm[i]));
  605.       
  606.       
  607.       v->lW=v->W;
  608.       v->W=v->nW;
  609.       v->centerW=new_centerNext;
  610.       
  611.       if(v->eofflag){
  612.     v->eofflag-=movementW;
  613.     /* do not add padding to end of stream! */
  614.     if(v->centerW>=v->eofflag){
  615.       v->granulepos+=movementW-(v->centerW-v->eofflag);
  616.     }else{
  617.       v->granulepos+=movementW;
  618.     }
  619.       }else{
  620.     v->granulepos+=movementW;
  621.       }
  622.     }
  623.   }
  624.  
  625.   /* done */
  626.   return(1);
  627. }
  628.  
  629. int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
  630.   _vds_shared_init(v,vi,0);
  631.  
  632.   v->pcm_returned=-1;
  633.   v->granulepos=-1;
  634.   v->sequence=-1;
  635.  
  636.   return(0);
  637. }
  638.  
  639. /* Unlike in analysis, the window is only partially applied for each
  640.    block.  The time domain envelope is not yet handled at the point of
  641.    calling (as it relies on the previous block). */
  642.  
  643. int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
  644.   vorbis_info *vi=v->vi;
  645.   codec_setup_info *ci=vi->codec_setup;
  646.  
  647.   /* Shift out any PCM that we returned previously */
  648.   /* centerW is currently the center of the last block added */
  649.  
  650.   if(v->centerW>ci->blocksizes[1]/2 &&
  651.   /* Quick additional hack; to avoid *alot* of shifts, use an
  652.      oversized buffer.  This increases memory usage, but doesn't make
  653.      much difference wrt L1/L2 cache pressure. */
  654.      v->pcm_returned>8192){
  655.  
  656.     /* don't shift too much; we need to have a minimum PCM buffer of
  657.        1/2 long block */
  658.  
  659.     int shiftPCM=v->centerW-ci->blocksizes[1]/2;
  660.     shiftPCM=(v->pcm_returned<shiftPCM?v->pcm_returned:shiftPCM);
  661.  
  662.     v->pcm_current-=shiftPCM;
  663.     v->centerW-=shiftPCM;
  664.     v->pcm_returned-=shiftPCM;
  665.     
  666.     if(shiftPCM){
  667.       int i;
  668.       for(i=0;i<vi->channels;i++)
  669.     memmove(v->pcm[i],v->pcm[i]+shiftPCM,
  670.         v->pcm_current*sizeof(*v->pcm[i]));
  671.     }
  672.   }
  673.  
  674.   v->lW=v->W;
  675.   v->W=vb->W;
  676.   v->nW=-1;
  677.  
  678.   v->glue_bits+=vb->glue_bits;
  679.   v->time_bits+=vb->time_bits;
  680.   v->floor_bits+=vb->floor_bits;
  681.   v->res_bits+=vb->res_bits;
  682.  
  683.   if(v->sequence+1 != vb->sequence)v->granulepos=-1; /* out of sequence;
  684.                                                      lose count */
  685.  
  686.   v->sequence=vb->sequence;
  687.  
  688.   {
  689.     int sizeW=ci->blocksizes[v->W];
  690.     int centerW=v->centerW+ci->blocksizes[v->lW]/4+sizeW/4;
  691.     int beginW=centerW-sizeW/2;
  692.     int endW=beginW+sizeW;
  693.     int beginSl;
  694.     int endSl;
  695.     int i,j;
  696.  
  697.     /* Do we have enough PCM/mult storage for the block? */
  698.     if(endW>v->pcm_storage){
  699.       /* expand the storage */
  700.       v->pcm_storage=endW+ci->blocksizes[1];
  701.    
  702.       for(i=0;i<vi->channels;i++)
  703.     v->pcm[i]=_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i])); 
  704.     }
  705.  
  706.     /* overlap/add PCM */
  707.  
  708.     switch((int)v->W){
  709.     case 0:
  710.       beginSl=0;
  711.       endSl=ci->blocksizes[0]/2;
  712.       break;
  713.     case 1:
  714.       beginSl=ci->blocksizes[1]/4-ci->blocksizes[v->lW]/4;
  715.       endSl=beginSl+ci->blocksizes[v->lW]/2;
  716.       break;
  717.     default:
  718.       return(-1);
  719.     }
  720.  
  721.     for(j=0;j<vi->channels;j++){
  722.       float *pcm=v->pcm[j]+beginW;
  723.       float *p=vb->pcm[j];
  724.  
  725.       /* the overlap/add section */
  726.       for(i=beginSl;i<endSl;i++)
  727.     pcm[i]+=p[i];
  728.       /* the remaining section */
  729.       for(;i<sizeW;i++)
  730.     pcm[i]=p[i];
  731.  
  732.     }
  733.  
  734.     /* deal with initial packet state; we do this using the explicit
  735.        pcm_returned==-1 flag otherwise we're sensitive to first block
  736.        being short or long */
  737.  
  738.     if(v->pcm_returned==-1)
  739.       v->pcm_returned=centerW;
  740.  
  741.     /* track the frame number... This is for convenience, but also
  742.        making sure our last packet doesn't end with added padding.  If
  743.        the last packet is partial, the number of samples we'll have to
  744.        return will be past the vb->granulepos.
  745.        
  746.        This is not foolproof!  It will be confused if we begin
  747.        decoding at the last page after a seek or hole.  In that case,
  748.        we don't have a starting point to judge where the last frame
  749.        is.  For this reason, vorbisfile will always try to make sure
  750.        it reads the last two marked pages in proper sequence */
  751.  
  752.     if(v->granulepos==-1)
  753.       if(vb->granulepos==-1){
  754.     v->granulepos=0;
  755.       }else{
  756.     v->granulepos=vb->granulepos;
  757.       }
  758.     else{
  759.       v->granulepos+=(centerW-v->centerW);
  760.       if(vb->granulepos!=-1 && v->granulepos!=vb->granulepos){
  761.  
  762.     if(v->granulepos>vb->granulepos){
  763.       long extra=v->granulepos-vb->granulepos;
  764.  
  765.       if(vb->eofflag){
  766.         /* partial last frame.  Strip the extra samples off */
  767.         centerW-=extra;
  768.       }else if(vb->sequence == 1){
  769.         /* ^^^ argh, this can be 1 from seeking! */
  770.  
  771.  
  772.         /* partial first frame.  Discard extra leading samples */
  773.         v->pcm_returned+=extra;
  774.         if(v->pcm_returned>centerW)v->pcm_returned=centerW;
  775.         
  776.       }
  777.       
  778.     }/* else{ Shouldn't happen *unless* the bitstream is out of
  779.         spec.  Either way, believe the bitstream } */
  780.     v->granulepos=vb->granulepos;
  781.       }
  782.     }
  783.  
  784.     /* Update, cleanup */
  785.  
  786.     v->centerW=centerW;
  787.     v->pcm_current=endW;
  788.  
  789.     if(vb->eofflag)v->eofflag=1;
  790.   }
  791.  
  792.   return(0);
  793. }
  794.  
  795. /* pcm==NULL indicates we just want the pending samples, no more */
  796. int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm){
  797.   vorbis_info *vi=v->vi;
  798.   if(v->pcm_returned>-1 && v->pcm_returned<v->centerW){
  799.     if(pcm){
  800.       int i;
  801.       for(i=0;i<vi->channels;i++)
  802.     v->pcmret[i]=v->pcm[i]+v->pcm_returned;
  803.       *pcm=v->pcmret;
  804.     }
  805.     return(v->centerW-v->pcm_returned);
  806.   }
  807.   return(0);
  808. }
  809.  
  810. int vorbis_synthesis_read(vorbis_dsp_state *v,int bytes){
  811.   if(bytes && v->pcm_returned+bytes>v->centerW)return(OV_EINVAL);
  812.   v->pcm_returned+=bytes;
  813.   return(0);
  814. }
  815.  
  816.